package com.lingjtx.common.security.config;

import com.lingjtx.common.security.limit.ApiContext;
import com.lingjtx.common.security.limit.ApiLimitedState;
import com.lingjtx.common.security.limit.ApiSignatureState;
import com.lingjtx.common.security.limit.NonRepeatableState;
import com.lingjtx.common.security.strategy.NotLoginStrategy;
import com.lingjtx.common.security.strategy.OfflineStrategy;
import com.lingjtx.common.security.strategy.TokenContext;
import com.lingjtx.common.security.strategy.TokenExpireStrategy;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuthSecurityConfig {

    private final ApiLimitedState apiLimitedState;
    private final ApiSignatureState apiSignatureState;
    private final NonRepeatableState nonRepeatableState;

    private final NotLoginStrategy notLoginStrategy;
    private final OfflineStrategy offlineStrategy;
    private final TokenExpireStrategy tokenExpireStrategy;

    public AuthSecurityConfig(ApiLimitedState apiLimitedState, ApiSignatureState apiSignatureState,
                              NonRepeatableState nonRepeatableState, NotLoginStrategy notLoginStrategy,
                              OfflineStrategy offlineStrategy, TokenExpireStrategy tokenExpireStrategy) {
        this.apiLimitedState = apiLimitedState;
        this.apiSignatureState = apiSignatureState;
        this.nonRepeatableState = nonRepeatableState;
        this.notLoginStrategy = notLoginStrategy;
        this.offlineStrategy = offlineStrategy;
        this.tokenExpireStrategy = tokenExpireStrategy;
    }

    @Bean("apiStateContext")
    public ApiContext<ProceedingJoinPoint, HttpServletRequest> apiContext() {
        ApiContext<ProceedingJoinPoint, HttpServletRequest> context = new ApiContext<>();
        context.setHandlers(apiLimitedState);
        context.setHandlers(nonRepeatableState);
        context.setHandlers(apiSignatureState);
        return context;
    }

    @Bean
    public TokenContext tokenContext() {
        TokenContext context = new TokenContext();
        context.setHandlers(notLoginStrategy); // 1、首先判断没有登陆（有的情况就是刷新token也过期）
        context.setHandlers(offlineStrategy); // 2、再次判断被踢下线
        context.setHandlers(tokenExpireStrategy); // 3、后面要排除的是token过期
        return context;
    }
}
